////////////////////////////////////////////////////////////////////////////////
//
//  The MIT License
//
//  Copyright (c) 2008 - 2009. Dimarik
//
//  Permission is hereby granted, free of charge, to any person obtaining a copy
//  of this software and associated documentation files (the "Software"), to deal
//  in the Software without restriction, including without limitation the rights
//  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//  copies of the Software, and to permit persons to whom the Software is
//  furnished to do so, subject to the following conditions:
//
//  The above copyright notice and this permission notice shall be included in
//  all copies or substantial portions of the Software.
//
//  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//  THE SOFTWARE.
//
////////////////////////////////////////////////////////////////////////////////

package ru.goodcore.logging.formatters {

import ru.goodcore.logging.LogEvent;
import ru.goodcore.logging.LogEventLevel;
import ru.goodcore.utils.StringUtils;
import ru.goodcore.logging.ILogger;

//import ru.goodcore.logging.ILogger;

/**
 *
 *
 * @author					Dimarik
 * @version					1.0
 * @playerversion			Flash 9
 * @langversion				3.0
 *
 * @keyword					formatter, htmlformatter
 */
public class HTMLFormatter extends LineFormatter {

	//-------------------------------------------------------------------------
	//
	//  Constructor
	//
	//-------------------------------------------------------------------------

	/**
	 * Constructor
	 */
	public function HTMLFormatter() {
		super();
	}

	//-------------------------------------------------------------------------
	//
	//  Public methods. Interface implementation: ITargetMessageFormatter
	//
	//-------------------------------------------------------------------------

	public override function formatMessage(event:LogEvent):String {
		var message:		String = '<p><font face="_typewriter">';

		var date:			String = '';
		var d:				Date = new Date();
		var currentTime:	Number = d.getTime();

		if (
			super.includeDate || 
			super.includeTime || 
			super.includeMeasuredTime
		) {

			if (super.includeDate) {
				date = Number(d.getMonth() + 1).toString() + '/' +
						d.getDate().toString() + '/' + 
						d.getFullYear() + super.fieldSeparator;
			}

			if (super.includeTime) {
				date += super.padTime(d.getHours()) + ':' +
						super.padTime(d.getMinutes()) + ':' +
						super.padTime(d.getSeconds()) + '.' +
						super.padTime(d.getMilliseconds(), true) + 
						super.fieldSeparator;
			}

			if (super.includeMeasuredTime) {

				date += '+' + StringUtils.setNumberLen(currentTime - super.lastMeasuredTime, 5) + 
						super.fieldSeparator;
			}

			message += '<font color="#FFFFFF">' + date + '</font>';
		}

		var level:	String = '';
		var color:	uint = this.getColorByLevel(event.level);

		message += '<font color="#' + color.toString(16) + '">';

		if (super.includeLevel) {
			level = '<b>[' + 
					LogEvent.getLevelString(event.level) +
					']' + '</b>' + super.fieldSeparator;

			message += level;
		}

		var category:String = super.includeCategory ?
			'<b>' + ILogger(event.target).category + '</b>' +  super.fieldSeparator : '';

		message +=  category ;

		message += StringUtils.encodeHTML(event.message) + '</font></font></p>';

		super.lastMeasuredTime = d.getTime();

		return message;
	}

	//-------------------------------------------------------------------------
	//
	//  Private methods
	//
	//-------------------------------------------------------------------------

	/**
	 *  @private
	 */
	private function getColorByLevel(value:uint):uint {
		switch (value) {
			case LogEventLevel.INFO:
				return 0x72FD0B;

			case LogEventLevel.DEBUG:
				return 0xF9ED59;

			case LogEventLevel.ERROR:
				return 0xFF8040;

			case LogEventLevel.WARN:
				return 0xFF00FF;

			case LogEventLevel.FATAL:
				return 0xFF0000;

			case LogEventLevel.ALL:
			default:
				return 0xFFFFFF;
		}
	}

}
}